﻿package com.tvein.sanguo.swc.chat
{
	//flash
	import flash.display.MovieClip;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import fl.events.ComponentEvent;
	//A3D
	//custom
	import com.tvein.sanguo.swc.common.SwcEvent;
	import com.tvein.sanguo.swc.common.MP3Click;
//
	public class ChatShell extends MovieClip
	{
		public static const NAME:String = "ChatShell";
		
		private var effectVol:int = 100;
		
		private var intChannel:int = 5;
		
		public function ChatShell()
		{
			stage?init(null):addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(evt:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE , init);
			addEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			stage.addEventListener(Event.RESIZE, _resizeUI);
			
			btnList.visible = false;
			
			var style:TextFormat = new TextFormat();
			style.size = 16;
			style.font = "LiSu";
			style.color = 0x467E1C;
			
			mcInput.txtInput.setStyle("textFormat", style);
			mcInput.txtTips.visible = false;
			mcInput.txtTo.visible = false;
			mcInput.txtTo.selectable = false;
			
			mcInput.btnChannel5.buttonMode = true;
			mcInput.btnChannel6.buttonMode = true;
			mcInput.btnChannel7.buttonMode = true;
			mcInput.btnChannel8.buttonMode = true;
			
			btnList.addEventListener(MouseEvent.MOUSE_OUT, onbtnListOut);
			btnList.addEventListener(MouseEvent.MOUSE_OVER, onbtnListOn);
			
			btnList.btnChannel5.addEventListener(MouseEvent.CLICK, onChannel);
			btnList.btnChannel6.addEventListener(MouseEvent.CLICK, onChannel);
			btnList.btnChannel7.addEventListener(MouseEvent.CLICK, onChannel);
			btnList.btnChannel8.addEventListener(MouseEvent.CLICK, onChannel);
			
			mcInput.btnChannel5.addEventListener(MouseEvent.CLICK, onbtnList);
			mcInput.btnChannel6.addEventListener(MouseEvent.CLICK, onbtnList);
			mcInput.btnChannel7.addEventListener(MouseEvent.CLICK, onbtnList);
			mcInput.btnChannel8.addEventListener(MouseEvent.CLICK, onbtnList);
			
			mcInput.addEventListener(ComponentEvent.ENTER, _inputEnter); 
			
			_resizeUI();
		}
		
		public function console(Type:String, params:Object):void
		{
			switch (Type)
			{
				case "setEffect":
					effectVol = params.Volume;
					break;
				case "setChat":
					var style:TextFormat = new TextFormat();
					style.size = 16;
					style.font = "LiSu";
					style.color = 0x803C1C;
					
					mcInput.setChildIndex(mcInput.btnChannel8, mcInput.numChildren - 1);
					mcInput.txtInput.setStyle("textFormat", style);
					
					var Tips:TextFormat = new TextFormat();
					Tips.size = 16;
					Tips.font = "LiSu";
					Tips.align = "center";
					Tips.italic = true;
					Tips.color = 0xFF0099;
					
					mcInput.txtTips.setStyle("textFormat", Tips);
					mcInput.txtTips.text = params;
					
					mcInput.txtTips.visible = true;
					mcInput.txtTo.visible = true;
					intChannel = 8;
					
					break;
			}
		}
		
		private function onChannel(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			mcInput.txtTips.visible = false;
			mcInput.txtTo.visible = false;
			
			var style:TextFormat = new TextFormat();
			style.size = 16;
			style.font = "LiSu";
			
			switch(evt.target.name)
			{
				case "btnChannel5":
					mcInput.setChildIndex(mcInput.btnChannel5, mcInput.numChildren - 1);
					style.color = 0x467E1C;
					mcInput.txtInput.setStyle("textFormat", style);
					intChannel = 5;
					break;
				case "btnChannel6":
					mcInput.setChildIndex(mcInput.btnChannel6, mcInput.numChildren - 1);
					style.color = 0x216371;
					mcInput.txtInput.setStyle("textFormat", style);
					intChannel = 6;
					break;
				case "btnChannel7":
					mcInput.setChildIndex(mcInput.btnChannel7, mcInput.numChildren - 1);
					style.color = 0x7D661C;
					mcInput.txtInput.setStyle("textFormat", style);
					intChannel = 7;
					break;
				case "btnChannel8":
					mcInput.setChildIndex(mcInput.btnChannel8, mcInput.numChildren - 1);
					style.color = 0x803C1C;
					mcInput.txtInput.setStyle("textFormat", style);
					
					var Tips:TextFormat = new TextFormat();
					Tips.size = 16;
					Tips.font = "LiSu";
					Tips.align = "center";
					Tips.italic = true;
					Tips.color = 0xFF0099;
					
					mcInput.txtTips.setStyle("textFormat", Tips);
					mcInput.txtTips.text = "";
					
					mcInput.txtTips.visible = true;
					mcInput.txtTo.visible = true;
					intChannel = 8;
					break;
			}
			
			btnList.visible = false;
		}
		
		private function onbtnListOut(evt:MouseEvent):void
		{
			btnList.visible = false;
		}
		
		private function onbtnListOn(evt:MouseEvent):void
		{			
			btnList.visible = true;
		}
		
		private function onbtnList(evt:MouseEvent):void
		{
			var mp3Click:MP3Click = new MP3Click();
			mp3Click.playback(effectVol * .01);
			
			if (btnList.visible)
			{
				btnList.visible = false;
			}
			else
			{
				btnList.visible = true;
			}
		}
		
		private function _inputEnter(evt:ComponentEvent):void
		{
			if (evt.target.text != "")
			{
				var mp3Click:MP3Click = new MP3Click();
				mp3Click.playback(effectVol * .01);
				
				dispatchEvent(new SwcEvent(NAME, ["RoleCall(" + intChannel + ", RoleChat, " + mcInput.txtTips.text + ", " + escape(evt.target.text) + ");"]));
			}
			
			evt.target.text = "";
		}
		
		private function _onRemoved(evt:Event):void
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, _onRemoved);
			stage.removeEventListener(Event.RESIZE, _resizeUI);
		}
		
		private function _resizeUI(evt:Event = null):void
		{
			mcInput.x = 10;
			mcInput.y = stage.stageHeight - 58;
			
			btnList.x = 10;
			btnList.y = stage.stageHeight - 180;
		}
	}
}